{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# GEV VS POT\n",
    "# First, GEV\n",
    "# If xi is greater than zero, the GEV becomes the Frechet distribution\n",
    "# If xi equals to zero, the GEV becomes the Gumbel distribution\n",
    "# If xi is less than zero, the GEV becomes the Weibull distribution\n",
    "# Example 1 Gumbel quantiles\n",
    "def gumbel_quantile(mu=0, sigma=1, p=0.95):\n",
    "    return mu-sigma * np.log(-np.log(p))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.9701952490421637"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gumbel_quantile()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1.0971887003649488"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gumbel_quantile(p=0.05)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Example 2 Frechet quantiles\n",
    "def frechet_quantile(mu=0, sigma=1, xi=.2 ,p=0.95):\n",
    "    return mu - (1-np.power(-np.log(p),-xi))*sigma/xi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-0.9851492550017243"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frechet_quantile(p=.05)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4.056447746787513"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frechet_quantile()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4.792362609405395"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frechet_quantile(xi=0.3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.6906419565469336"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Example 3 Gumbel VaR\n",
    "# For the standardised Gumbel and n =100, the 99.5% VaR is\n",
    "alpha = .995\n",
    "n = 100\n",
    "gumbel_quantile(p=np.power(alpha, n))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.3020848845356237"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# The 99.9% VaR is\n",
    "alpha = .999\n",
    "gumbel_quantile(p=np.power(alpha, n))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.7406147491021509"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Example 4 Frechet VaR\n",
    "# For the standardised Frechet with xi = 0,2 and n =100, the 99.5% VaR is\n",
    "alpha = .995\n",
    "n = 100\n",
    "xi = 0.2\n",
    "frechet_quantile(xi=0.2, p=np.power(alpha, n))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.9236732249790562"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# The 99.9% VaR is \n",
    "alpha = .999\n",
    "frechet_quantile(xi=0.2, p=np.power(alpha, n))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.76739824, 3.31654308])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# For xi = 0.3, the 99.5% and 99.9% VaRs are\n",
    "alpha = np.array([.995, .999])\n",
    "frechet_quantile(xi=0.3, p=np.power(alpha, n))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "99.5% VaR is 2.5371787708033926.\n",
      "99.9% VaR is 4.321580154383378.\n"
     ]
    }
   ],
   "source": [
    "# Example 5 Realistic Frechet VaR\n",
    "# For US stock market, some fairly plausible parameters are mu=2%, sigma=0.7% and xi=0.3%\n",
    "# n = 100\n",
    "# the estimated 99.5% VaR (in %) is\n",
    "print(\"99.5% VaR is {0}.\".format(frechet_quantile(mu=2, sigma=.7, xi=.3, p=np.power(.995, n))))\n",
    "print(\"99.9% VaR is {0}.\".format(frechet_quantile(mu=2, sigma=.7, xi=.3, p=np.power(.999, n))))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "# POT peeks-over-threshold\n",
    "# The generalised pareto distribution\n",
    "# POT risk measures\n",
    "# The VaR is given by POT\n",
    "def POT_VaR(u, beta, xi, p, n, N):\n",
    "    return u + beta/xi*(np.power(n/N*(1-p),-xi)-1)\n",
    "# The ES is given by POT (xi < 1)\n",
    "def POT_ES(u, beta, xi, p, n, N):\n",
    "    VaR = POT_VaR(u, beta, xi, p, n, N)\n",
    "    return VaR/(1-xi) + (beta-xi*u)/(1-xi)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.952214702690108"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "POT_VaR(u=2, beta=.8, xi=.15, p=.995, n=100, N=4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5.237899650223656"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "POT_ES(u=2, beta=.8, xi=.15, p=.995, n=100, N=4)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
